package com.atguigu.wordcount.commonfriend;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Arrays;

/**
 * @ClassName MapperCF2
 * @DeScription MapperCF2
 * @Author 剧情再美终是戏
 * @Date 2019/11/20 18:13
 * @Version 1.0
 **/
// 输入 B:A,C,D
// 输出  A-C:B A-D:B
public class MapperCF2 extends Mapper<Text, Text, Text, Text> {

    private Text out_key = new Text();

    @Override
    protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
        String[] split = value.toString().split(",");

        // 进行一次排序防止A-B B-A这个共同好友失效的问题
        Arrays.sort(split);

        // 循环组装这个好友属于哪两两用户
        for (int i = 0; i < split.length - 1; i++) {
            for (int j = i + 1; j < split.length; j++) {
                out_key.set(split[i] + "-" + split[j]);
                context.write(out_key, key);
            }
        }
    }
}
